var express = require('express');
var router = express.Router();
const multiparty = require('multiparty')
const fs = require('fs')
const path = require('path')
const moment = require('moment')


/*视图路由 */
//登陆页面
router.get('/', function (req, res, next) {
    res.render('admin_tmpl/login.html', {});
});

//管理页面
router.get('/maim', function (req, res, next) {
    if (req.session.username) {
    res.render('admin_tmpl/main.html', {user: req.session.username, avatar: req.session.avatar});
    } else {
        res.redirect('/admin');
    }

});

//首页
router.get('/index', function (req, res, next) {
    res.render('admin_tmpl/index.html', {});
});

//管理员管理列表
router.get('/list', function (req, res, next) {
    res.render('admin_tmpl/admin/list.html', {});
});

//用户修改密码路由
router.get('/password', function (req, res, next) {
    res.render('admin_tmpl/password.html', {id: req.session.adminId});
});

//管理员管理修改密码理由
router.get('/updatePassword', function (req, res, next) {
    var id = req.query.id;
    res.render('admin_tmpl/admin/updatePassword.html', {id: id});
});

//管理员管理新增管理员理由
router.get('/add', function (req, res, next) {
    var id = req.query.id;
    res.render('admin_tmpl/admin/add.html', {id: id});
});

//管理员修改信息
router.get('/edit', async (req, res, next) => {
    var id = req.query.id;
    var selectSQL = 'select * from t_admin where delete_flag = 0 and id="' + id + '"'
    await common.prototype.operation(selectSQL).then(rest => {
        res.render('admin_tmpl/admin/edit.html', {id: id, username: rest[0].username});
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })

});

//基本资料
router.get('/info', async (req, res, next) => {
    var id = req.session.adminId;
    var selectSQL = 'select * from t_admin where delete_flag = 0 and id="' + id + '"'
    await common.prototype.operation(selectSQL).then(rest => {
        res.render('admin_tmpl/info.html',
            {id: id, username: rest[0].username, avatar: rest[0].avatar, roleType: rest[0].role_type});
    }, (error) => {
        console.log(error)
        res.send(error)
    }).catch((err) => {
        console.log(err)
    })
});

/*******************************************************接口*******************************************************************/

/*
 * 登录
 *
 * @param username
 * @param password
 * @return
 *
 */
router.post('/login', async (req, res, next) => {
    var data = req.body;
    console.log(req.session)
    var selectSQL = 'select * from t_admin where delete_flag = 0 and username="' + data.username + '" and password="' + data.password + '" '
    await common.prototype.operation(selectSQL).then(rest => {
        if (rest.length > 0) {
            req.session.username = rest[0].username;
            req.session.adminId = rest[0].id;
            req.session.avatar = rest[0].avatar;
            res.send({
                type: 'success', content: '登陆成功，正在跳转...',
            })
        } else {
            res.send({
                type: 'error',
                content: '用户名或者密码错误！'
            })
        }

    }, (error) => {
        console.log(error)
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }).catch((err) => {
        console.log(err)
    })
});

//退出登录
router.get('/logout', function (req, res, next) {
    req.session.username = null;
    res.redirect('/admin');
});



/*
 * 获取管理员列表
 * @return
 *
 */
router.post('/getList', async (req, res, next) => {
    var condition = '';
    var SQL = '';
    var conditionSQL = '';
    var allData = '';
    var begin = 0;
    var data = req.body;
    var pageSize = data.pageSize;
    var pageNumber = data.pageNumber;
    var likeAdmin = req.body.search_like_username;
    if (pageSize) {
        begin = (pageNumber - 1) * pageSize;
        condition = condition + 'order by id desc limit ' + begin + ','+pageSize+''
    }

    if (likeAdmin) {
        SQL = 'select * from t_admin where delete_flag = 0 and username like "%' + likeAdmin + '%"'
    } else {
        SQL = 'select * from t_admin where delete_flag = 0'
    }

    await common.prototype.operation(SQL).then(async rest => {
        allData = rest;
        if (likeAdmin) {
            conditionSQL = 'select * from t_admin where delete_flag = 0 and username like  "%' + likeAdmin + '%"' + condition + ''
        } else {
            conditionSQL = 'select * from t_admin where delete_flag = 0 ' + condition + ''
        }
        await common.prototype.operation(conditionSQL).then(async result => {
            res.send({
                code: 0, msg: '操作成功',
                count: allData.length, data: result
            })
        })
    }, (error) => {
        console.log(error)
        res.send({
            code: 404, msg: '操作失败',
            data: error
        })
    }).catch((err) => {
        console.log(err)
    })
});


/*
 * 添加管理员
 *
 * @param id
 * @param username
 * @param password
 * @param roleType
 * @return
 *
 */
router.post('/addAdmin', async (req, res, next) => {
    var data = req.body;
    if (data.password) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'INSERT INTO `t_admin`(id, username, password, role_type,create_date,modify_date,delete_flag) VALUES(0,"' + data.username + '","' + data.password + '","' + data.roleType + '","' + date + '","' + date + '",0) '
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '添加成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '添加失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});


/*
 * 修改信息
 *
 */
router.post('/updateInfo', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'update t_admin set avatar="' + data.avatar + '", modify_date="' + date + '" where id="' + data.id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '修改成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '修改失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});


/*
 * 修改密码
 *
 * @param id
 * @param oldPassword
 * @param newPassword
 * @return
 *
 */
router.post('/changePassword', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'update t_admin set password="' + data.newPassword + '", modify_date="' + date + '" where id="' + data.id + '" and password="' + data.oldPassword + '" '
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '修改成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '修改失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});

/*
 * 修改用户名
 *
 * @param id
 * @param oldPassword
 * @param newPassword
 * @return
 *
 */
router.post('/update', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'update t_admin set username="' + data.username + '", modify_date="' + date + '" where id="' + data.id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '修改成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '修改失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});

module.exports = router;